/*  -------------------------- (C) COPYRIGHT 2022 Fortiortech ShenZhen ---------------------------*/
/**
    @copyright (C) COPYRIGHT 2022 Fortiortech Shenzhen
    @file      xxx.c
    @author    Fortiortech  Appliction Team
    @since     Create:2022-07-14
    @date      Last modify:2022-08-07
    @note      Last modify author is Leo.li
    @brief
*/

/* Includes -------------------------------------------------------------------------------------*/
#include <FU68xx_2.h>
#include <Myproject.h>
#include <math.h>

extern uint8 data g_1mTick; ///< 1ms滴答信号，每隔1ms在SYSTICK定时器被置1，需在大循环使用处清零

uint16 xdata spidebug[4] = {0};

/**
    @brief        FO_INT interrupt，硬件FO过流保护，关断输出，中断优先级最高
    @date         2022-08-07
*/
void FO_INT(void) interrupt 1 // 硬件FO过流中断，关闭输出
{
    MOE = 0;                            // 关闭输出
    mcFaultSource = FaultHardOVCurrent; // 硬件过流保护
    IF0 = 0;                            // clear P00 interrupt flag
}

/**
    @brief        外部中断：用于过零检测和发热控制
    @date         2022-08-07
*/
void EXTERN_INT(void) interrupt 2
{

    mcFocCtrl.ZeroCnt = 0;
    if (mcFocCtrl.ZeroLossFlag > 0)
    {
        mcFocCtrl.ZeroLossFlag--;
        if (mcFocCtrl.ZeroLossFlag == 1)
        {
            mcFaultSource = FaultNoSource; // 解决关机快速重启的问题。
        }
    }

    ClrBit(TIM2_CR1, T2EN); //
    TIM2__ARR = 18000;      // 5ms延迟
    TIM2__DR = TIM2__ARR;
    TIM2__CNTR = 0;
    SetBit(TIM2_CR1, T2EN);
    //	if(mcFocCtrl.zeroResume>0)
    //	{
    //		mcFocCtrl.zeroResume--;
    //		if(mcFocCtrl.zeroResume==0)//重新上电
    //		{
    ////		    KS.Key1Value = 0x01;//掉电恢复冷风档
    ////			KS.Key2Value = 0x20;
    ////			KS.KeyValuetotal=0x21;
    ////			KS.ChangeKeyFlg = 1;
    //		}
    //	}
    //	else
    //	{
    //		mcFocCtrl.ZeroCnt=0;
    //		mcFocCtrl.ZeroLossFlag = 0;
    //	}

    // static uint16 ScrOnTime = 0; // 发热开启时间
    // zeroCrossTest();
    //    mcFocCtrl.ZeroCnt = 0;
    //    mcFocCtrl.ZeroFlag = 1;
    //    mcFocCtrl.ZeroLossCnt = 0;
    //    mcFocCtrl.ZeroLossFlag = 0;
    //            ClrBit(TIM2_CR1, T2EN); //
    //            TIM2__ARR = 18000;      // 6ms延迟
    //            TIM2__DR = TIM2__ARR;
    //            TIM2__CNTR = 0;
    //            SetBit(TIM2_CR1, T2EN);
    // #if (START_MODE == PLL_Start)
    //     {
    //         if (PLLfunction.PLLFunctionFlag > 2)
    //         {
    //             ClrBit(TIM2_CR1, T2EN); //
    //             TIM2__ARR = 18000;      // 6ms延迟
    //             TIM2__DR = TIM2__ARR;
    //             TIM2__CNTR = 0;
    //             SetBit(TIM2_CR1, T2EN);
    //         }
    //         else
    //         {
    //             ClrBit(TIM2_CR1, T2EN); //
    //         }
    //     }
    //  #endif
    ClrBit(P1_IF, P15); // 清零中断标志位
}

/**
    @brief        Time2-BEMF顺逆风检测
    @date         2022-08-07
*/
void TIM2_INT(void) interrupt 4
{
    if (ReadBit(TIM2_CR1, T2IR))
    {
        ClrBit(TIM2_CR1, T2IR);
    }

    if (ReadBit(TIM2_CR1, T2IP))
    {
        ClrBit(TIM2_CR1, T2IP);
    }

    if (ReadBit(TIM2_CR1, T2IF))
    {
        // tempSet(); // 丢波
        // User.tempCnt = 0;
        // ClrBit(TIM2_CR1, T2EN); // 关闭计数器
        // User.tempCnt = 0;
        GP01 = 1; // 关闭发热丝
        tempSet1();
        ClrBit(TIM2_CR1, T2EN); // 关闭计数器
        ClrBit(TIM2_CR1, T2IF);
    }
}

/**
    @brief        CMP3：硬件比较器过流保护，关断输出，中断优先级最高
    @brief        CMP0/1/2：顺逆风判断
    @date         2022-07-14
*/
void CMP_ISR(void) interrupt 7
{
    if (ReadBit(CMP_SR, CMP3IF))
    {
        if (mcState != mcPosiCheck)
        {
            MOE = 0;                            // 关闭输出
            mcFaultSource = FaultHardOVCurrent; // 硬件过流保护
        }
        else
        {
            MOE = 0;
#if (PosCheckEnable)                                                                            // 关闭MOE
            RPDPara.InsetCount[RPDPara.injecttimes] = TIM2__CNTR;                               // 将定时器2的计数值赋值给数组
            RPDPara.DetectCount[RPDPara.injecttimes] = RPDPara.InsetCount[RPDPara.injecttimes]; // 两组数据，一组用于观察原始数据，一组用于处理数据
            TIM2__CNTR = 0;                                                                     // TIM2计数器值清零
            RPDPara.injecttimes++;                                                              // RPD注入拍数累加
#endif
        }

        ClrBit(CMP_SR, CMP3IF); // 清除比较器中断
    }

/*------通过BEMF做顺风启动功能 -----*/
#if ((FRDetectMethod == BEMFMethod) && (TailWind_Mode == TailWind))
    BEMFDetectFunc();
#endif
}

/**
    @brief        Capture PWM ，用于PWM调速
    @date         2022-08-07
*/
void TIM3_INT(void) interrupt 9
{

    if (ReadBit(TIM3_CR1, T3IR)) // 达到DR的时候触发
    {
        ClrBit(TIM3_CR1, T3IR);
    }

    if (ReadBit(TIM3_CR1, T3IP)) // 周期中断
    {
        ClrBit(TIM3_CR1, T3IP);
    }

    if (ReadBit(TIM3_CR1, T3IF)) // 达到ARR的时候触发
    {
        if ((mcFocCtrl.ZeroLossFlag == 0 && mcState == mcRun) || (mcFocCtrl.ZeroLossFlag == 0 && mcState == mcFault))
        {
            if (ledCt1.ledCounter < ledCt1.ledCycle)
            {
                ledCt1.ledCounter++;
            }
            else
            {
                ledCt1.ledCounter = 0;
            }

            if (ledCt1.ledCounter < ledCt1.RDuty)
            {
                R(ON);
            }
            else
            {
                R(OFF);
            }

            if (ledCt1.ledCounter < ledCt1.GDuty)
            {
                G(ON);
            }
            else
            {
                G(OFF);
            }

            if (ledCt1.ledCounter < ledCt1.BDuty)
            {
                B(ON);
            }
            else
            {
                B(OFF);
            }
        }
        else
        {
            LED_OFF;
        }

        // if (((mcState == mcRun) && (MCCtrl.FlagONOFF == 1)) || (mcFaultSource != FaultNoSource))
        // {
        //     if (ledCt1.ledCounter < ledCt1.ledCycle)
        //     {
        //         ledCt1.ledCounter++;
        //     }
        //     else
        //     {
        //         ledCt1.ledCounter = 0;
        //     }

        //     if (ledCt1.ledCounter < ledCt1.RDuty)
        //     {
        //         R(ON);
        //     }
        //     else
        //     {
        //         R(OFF);
        //     }

        //     if (ledCt1.ledCounter < ledCt1.GDuty)
        //     {
        //         G(ON);
        //     }
        //     else
        //     {
        //         G(OFF);
        //     }

        //     if (ledCt1.ledCounter < ledCt1.BDuty)
        //     {
        //         B(ON);
        //     }
        //     else
        //     {
        //         B(OFF);
        //     }
        // }
        // else
        // {
        //     LED_OFF;
        // }

        ClrBit(TIM3_CR1, T3IF);
    }
}

/**
    @brief        FOC中断(Drv中断), 可配置1 | 2 个载波周期执行一次，用于处理响应较高的程序
    @date         2022-08-07
*/
void FOC_INT(void) interrupt 3
{
    if (ReadBit(DRV_SR, DCIF)) // 比较中断
    {
#if (START_MODE == PLL_Start)
        {
            if ((mcState == mcStart) || (mcState == mcRun))
            {
                PLLStateFunction();
            }
        }
#endif
#if ((FRDetectMethod == FOCMethod) && (TailWind_Mode == TailWind))
        {
            TailWindSpeedDetect(); // 顺逆风检测
        }
#endif
        DRV_SR = (DRV_SR | SYSTIF) & (~DCIF);
    }
}

/**
    @brief        1ms定时器中断（SYS TICK中断），用于处理附加功能，如控制环路响应、各种保护等。
    @date         2022-08-07
*/
void TIM_1MS_INT(void) interrupt 10
{
    if (ReadBit(DRV_SR, SYSTIF)) // SYS TICK中断
    {
        g_1mTick = 1;
        SetBit(ADC_CR, ADCBSY); // 使能ADC的DCBUS采样
        WatchDogRefresh();
        DRV_SR = (DRV_SR | DCIF) & (~SYSTIF);
    }

    if (ReadBit(TIM4_CR1, T4IR))
    {
        ClrBit(TIM4_CR1, T4IR);
    }

    if (ReadBit(TIM4_CR1, T4IP)) // 周期中断
    {
        ClrBit(TIM4_CR1, T4IP);
    }

    if (ReadBit(TIM4_CR1, T4IF))
    {
        ClrBit(TIM4_CR1, T4IF);
    }
}

/**
    @brief        串口中断
    @date         2022-08-07
*/
void USART_INT(void) interrupt 12
{
    if (RI == 1)
    {
        RI = 0;
        Uart.Uredata = UT_DR; // 读接收数据
    }
}

/* Private variables ----------------------------------------------------------------------------*/
void INT0(void) interrupt 0
{
}
void INT5(void) interrupt 5
{
}
void INT6(void) interrupt 6
{
}
void INT8(void) interrupt 8
{
}
void INT11(void) interrupt 11
{
}
void INT13(void) interrupt 13
{
}
void INT14(void) interrupt 14
{
}
void INT15(void) interrupt 15
{
}
